(function() {
	var $ = require(L('$')), _ = require(L('_'));
	var win = $.createWindow(L('win_login'));
	win.add(new $.ui('navbar')(false, true));

	var view = Ti.UI.createView({
		top : L('nav_bar_height'),
		width : '100%',
		height : $.mainContentHeight(),
		layout : 'vertical',
		backgroundColor : '#fff'
	});
	win.add(view);
	win.display = function() {
		$.mask(true, true);
		$.removeChildren(view);
		$.webservice().getShoppingCartID(function(responseText, nothingNew) {
			var result = JSON.parse(responseText);
			var url = L('url_mobile_website') + '/OpenCheckoutWebsiteV2/' + result.ShoppingCartID + '/' + L('ios_platform');
			view.add(WebView(url));
		});
	};

	function WebView(url) {
		$.w('#URL = ' + url);
		var webView = Ti.UI.createWebView({
			url : url,
			width : '100%',
			backgroundColor : '#f6f6f6',
			scalesPageToFit : false,
			height : $.mainContentHeight(),
			hideShadow : true
		});
		var currentStep = 0;
		$.on('beforeload', webView, function(e) {
			$.w('#BEFORE_LOAD, step = ' + currentStep);
			if (currentStep != 0) {
				$.opacity_show(webView, false);
				$.mask();
			}
		});
		$.on('error', webView, function(e) {
			$.w('#ERROR = ' + JSON.stringify(e.source));
		});
		$.on('load', webView, function(e) {
			$.w('#LOADED');
			$.opacity_show(webView);
			$.mask(false);
			var webViewContent = e.source;
			if (webViewContent.isError) {
				var clientHeight = webViewContent.evalJS("document.getElementById('idMainContainer').offsetHeight;");
				var newHeight = $.scale(parseInt(clientHeight));
				var animationObj = Titanium.UI.createAnimation({
					height : newHeight,
					top : 0,
					duration : 200
				});
				animationObj.addEventListener('complete', function() {
					LoadingIndicator.hide();
				});
				webViewContent.animate(animationObj);
			}
			currentStep = webView.evalJS("document.getElementById('hidCurrentStep').value;");
			if (currentStep === '1') {
				var checkoutQuantityItemCount = webView.evalJS("document.getElementById('hidCheckoutQuantityCount').value;");
				$.updateBadges(checkoutQuantityItemCount, null);
			} else if (currentStep === '2') {
			} else if (currentStep === '3') {
			} else if (currentStep === '5') {
				var checkoutQuantityItemCount = webView.evalJS("document.getElementById('hidCheckoutQuantityCount').value;");
				$.updateBadges(checkoutQuantityItemCount, null);
				var nextAction = webView.evalJS("document.getElementById('idNextAction').value;");
				if (nextAction === '1') {
					//Barcode.scan();
				} else if (nextAction === '2') {
					$.openMyDesires();
				}
			} else if (currentStep === '7') {
				$.updateBadges(0, null);
			} else if (currentStep === '9') {
				//Open Terms and conditions
				$.win('my_account').nav.reset();
				$.win('my_account').hideView(false);
				$.ui('tabgroup').setActiveTab(4);
				$.win('setting_details').update(L('code_terms_and_conditions'), true, function() {
					$.win('my_cart').display();
					$.ui('tabgroup').setActiveTab(1);
				});
				$.win('my_account').nav.push($.win('setting_details'));
			} else if (currentStep === '10') {
				Ti.Platform.openURL(L('url_mobile_website') + "/ThawteSealCode");
			} else if (currentStep === '11') {
				Ti.Platform.openURL(webView.evalJS("document.getElementById('hidUrlSealEway').value;"));
			}
		});
		return webView;
	};

	module.exports = win;
})();
